package com.jbit.examSystem.controller;

import com.github.pagehelper.PageHelper;
import com.jbit.examSystem.pojo.*;
import com.jbit.examSystem.param.QuestionSearchParam;
import com.jbit.examSystem.param.PageParam;
import com.jbit.examSystem.service.ChapterService;
import com.jbit.examSystem.service.CourseService;
import com.jbit.examSystem.service.GradeService;
import com.jbit.examSystem.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;
    @Autowired
    private GradeService gradeService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private ChapterService chapterService;

    @RequestMapping("list")
    @ResponseBody
    public TableDataInfo list(QuestionSearchParam param, PageParam page,HttpSession session){
        TableDataInfo respData = new TableDataInfo();
        try {
            PageHelper.startPage(page.getPageNum(), page.getPageSize(), true, true, false);
            List<QuestionOe> questions = questionService.getAllQuestion(param);
            session.setAttribute("allQuestions",questions);
            respData.succeed(questions, 0);
        } catch (Exception e){
            respData.setCode(500);
            e.printStackTrace();
        }

        return respData;
    }

    @RequestMapping("addQuestion")
    public String addQuestion(Model model){
        List<Grade> grades = gradeService.getAllGrade();

        model.addAttribute("grades",grades);

        return "onlineExam/question/add";
    }

    @RequestMapping("getCourseByGradeId")
    @ResponseBody
    public List<Course> getCourseByGradeId(Integer gradeId){
        List<Course> courses = courseService.getCourseByGradeId(gradeId);

        return courses;
    }

    @RequestMapping("getChapterByCourseId")
    @ResponseBody
    public List<Chapter> getChapterByCourseId(Integer courseId){
        List<Chapter> chapters = chapterService.getChapterByCourseId(courseId);

        return chapters;
    }

    @RequestMapping("add")
    @ResponseBody
    public Map<String, Object> addQuestions(Question question){
        Map<String, Object> map = new HashMap<>();

        try {
             questionService.addQuestion(question);
             map.put("code", 0);
             map.put("msg", "试题添加成功");
        }catch (Exception e){
            map.put("code", 500);
            map.put("msg", "试题添加失败！原因是："+ e.getMessage());
        }
        return map;
    }

    @RequestMapping("delete")
    @ResponseBody
    public Map<String ,Object> delete(Integer[] ids){
        Map<String ,Object> map = new HashMap<>();

        try {
            questionService.delete(ids);
            map.put("code", 0);
            map.put("msg", "试题删除成功");
        }catch (Exception e){
            map.put("code", 500);
            map.put("msg", "试题删除失败！原因是："+ e.getMessage());
        }
        return map;
    }
}
